package com.example.newhabitclock.HabitRoomPackage

import android.content.Context
import androidx.lifecycle.LiveData
import com.example.newhabitclock.RecordRoomPackage.CheckInRecord

class HabitRepository(context:Context) {
    //HabitDatabase中有habitDao和checkInRecordDao
    private val habitDao:HabitDao = HabitDatabase.getHabitDatabase(context).getHabitDao()
    private val checkInRecordDao = HabitDatabase.getHabitDatabase(context).getCheckInRecordDao()

    suspend fun insertHabit(habit: Habit){
        habitDao.insert(habit)
    }

    fun getAllHabits():LiveData<List<Habit>>{
        return habitDao.getAllHabits()
    }

    //更新Room数据库中的信息
    suspend fun updateHabit(habit: Habit){
        habitDao.updateHabit(habit)
    }

    suspend fun deleteHabit(habit: Habit){
        habitDao.delete(habit)
    }

    fun getHabitById(habitId: Int):LiveData<Habit>{
        return habitDao.getHabitById(habitId)
    }

    suspend fun resetAllCheckIns(){
        habitDao.resetAllCheckIns()
    }

    //check_in_record表中的Dao方法
    //习惯插入打卡日期的方法
    suspend fun insertCheckInRecord(record:CheckInRecord){
        checkInRecordDao.insert(record)
    }

    //获取某习惯的打卡日期
    //输入该习惯的id，找到该习惯的所有打卡日期 List<String>
    fun getCheckInDates(habitId:Int):LiveData<List<String>>{
        return checkInRecordDao.getDatesByHabit(habitId)
    }


}